package com.wmapp.service.impl;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.springframework.util.StringUtils;

import com.wmapp.DomainConvertException;
import com.wmapp.connect.Request;
import com.wmapp.connect.SocketSupport;
import com.wmapp.domain.CDR;
import com.wmapp.domain.History;
import com.wmapp.domain.MCUStatistic;
import com.wmapp.domain.MCUVersion;
import com.wmapp.domain.PageResultWrapper;
import com.wmapp.service.ViewService;
import com.wmapp.utils.DomainUtils;
import com.wmapp.utils.SimpleStringUtils;
import java.util.List;
/**
 * This service is session scope.
 * @author dengl
 *
 */
public class ViewServiceImpl extends SocketSupport implements ViewService {
	private static Log log = LogFactory.getLog(ViewServiceImpl.class);
	
	@Override
	public MCUVersion getMCUVersion() {
		MCUVersion ret=null;
		
		try {
			String receiveXML=execute(Request.GET_VERSION_REQ);
			
			if(StringUtils.hasText(receiveXML)){
				Document doc=DocumentHelper.parseText(receiveXML);
				ret=(MCUVersion)DomainUtils.element2Domain(doc.getRootElement(), MCUVersion.class);
			}
		} catch (Exception e) {
			log.error(e.getMessage(), e);
			throw new DomainConvertException(e.getMessage());
		}
		
		return ret;
	}
	
	@Override
	public MCUStatistic getMCUStatistic() {
		MCUStatistic ret=null;
		
		try {
			String receiveXML=execute(Request.GET_STATICS_REQ);
			
			if(StringUtils.hasText(receiveXML)){
				Document doc=DocumentHelper.parseText(receiveXML);
				ret=(MCUStatistic)DomainUtils.element2Domain(doc.getRootElement(), MCUStatistic.class);
			}
		} catch (Exception e) {
			log.error(e.getMessage(), e);
			throw new DomainConvertException(e.getMessage());
		}
		
		return ret;
	}
	
	@Override
	public PageResultWrapper queryCDR(int start, int pagelimit,boolean browse, String start_date, String end_date, String caller, String called) {
		PageResultWrapper ret=new PageResultWrapper();
		Map params=new HashMap<String,String>();
		params.put("start", String.valueOf(start));
		params.put("page", String.valueOf(pagelimit));
        if (!browse) {
            if (!start_date.isEmpty()) {
                params.put("start_date", start_date);
            }
            if (!end_date.isEmpty()) {
                params.put("end_date", end_date);
            }
            if (!caller.isEmpty()) {
                params.put("caller", caller);
            }
            if (!called.isEmpty()) {
                params.put("called", called);
            }
        }
		String receiveXML=execute(Request.GET_CDR_REQ, params, Request.GET_CDR_REQ);
		try {
			if(StringUtils.hasText(receiveXML)){
				Document doc=DocumentHelper.parseText(receiveXML);
                ret.setTotal(Integer.valueOf(doc.selectSingleNode("/cdr/total").getText()));
                String s = doc.selectSingleNode("/cdr/billtime").getText();
                ret.setBilltime(s);
				ret.setResult(DomainUtils.xml2List(receiveXML, "/cdr/record", CDR.class));
			}
		} catch (DocumentException e) {
			log.error(e.getMessage(), e);
			throw new DomainConvertException(e.getMessage());
		}
		
		return ret;
	}

    @Override
	public List<History> queryHistory(String start_date, String end_date) {
		List<History> list=null;
		Map params=new HashMap<String,String>();
        if(!start_date.isEmpty())
		params.put("start_date", start_date);
        if(!end_date.isEmpty())
		params.put("end_date", end_date);
		String receiveXML=execute(Request.GET_HISTORY_REQ, params, Request.GET_HISTORY_REQ);
		try {
			if(StringUtils.hasText(receiveXML)){
				Document doc=DocumentHelper.parseText(receiveXML);
				list = DomainUtils.xml2List(receiveXML, "/history/item", History.class);
			}
		} catch (DocumentException e) {
			log.error(e.getMessage(), e);
			throw new DomainConvertException(e.getMessage());
		}

		return list;
	}
	
	@Override
	public String getMCUDatetime() {
		String receiveXML=execute(Request.GET_TIME_REQ);
		if(StringUtils.hasText(receiveXML)){
			try {
				Document doc=DocumentHelper.parseText(receiveXML);
				String date=doc.selectSingleNode("/datetime/date").getText();
				String time=doc.selectSingleNode("/datetime/time").getText();
				return SimpleStringUtils.join(date," ",time);
			} catch (DocumentException e) {
				log.error(e.getMessage(), e);
			}
		}
		return null;
	}
}
